//
//  232_用栈实现队列.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/1/24.
/**
    入队列：直接向inStack进行入栈操作
    出队列： 1、如果outStack不为空，直接outStack出栈
            2、 如果outStack为空， 将inStack中的元素全部入栈到outStack，然后在出栈第一个元素
 */

import UIKit

class MyQueue {

    let inStack = Stack<Int>() // 入栈
    let outStack = Stack<Int>() // 出栈
    init() {

    }
    func push(_ x: Int) {
        inStack.push(x)
    }
    
    func pop() -> Int? {
        if outStack.isEmpty() {
            while let elem = inStack.pop() {
                outStack.push(elem)
            }
        }
        return outStack.pop()
    }
    
    func peek() -> Int? {
        if outStack.isEmpty() {
            while let elem = inStack.pop() {
                outStack.push(elem)
            }
        }
        return outStack.top()
    }
    
    func empty() -> Bool {
        inStack.isEmpty() && outStack.isEmpty()
    }
}
